package cn.edu.scau.cmi.mengzhuorong.hibernate;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.edu.scau.cmi.mengzhuorong.hibernate.dao.StudentDAO;
import cn.edu.scau.cmi.mengzhuorong.hibernate.dao.TeacherDAO;
import cn.edu.scau.cmi.mengzhuorong.hibernate.domain.Student;
import cn.edu.scau.cmi.mengzhuorong.hibernate.domain.Teacher;

public class HibernateControl {
	
	static StudentDAO getStudentDAO() {
		ApplicationContext studentApplicationContext = new ClassPathXmlApplicationContext("cn\\edu\\scau\\cmi\\mengzhuorong\\hibernate\\dao\\SpringManageDAO.xml");
		return (StudentDAO) studentApplicationContext.getBean("studentId");
		
	}
	static TeacherDAO getTeacherDAO() {
		ApplicationContext teacherApplicationContext = new ClassPathXmlApplicationContext("cn\\edu\\scau\\cmi\\mengzhuorong\\hibernate\\dao\\SpringManageDAO.xml");
		return (TeacherDAO) teacherApplicationContext.getBean("teacherId");
	}
	
	public static void addStudent(Student student) {
		new StudentDAO().save(student);
	}
	
	public static void addTeacher(Teacher teacher) {
		new TeacherDAO().save(teacher);
	}
	
	public static void deleteStudent(Student student) {
		new StudentDAO().delete(student);
	}

	public static void deleteTeacher(Teacher teacher) {
		new TeacherDAO().delete(teacher);
	}
	
	public static Student findStudentById(Long id) {
		 return new StudentDAO().findById(id);
	}
	
	public static Teacher findTeacherById(Long id) {
		 return new TeacherDAO().findById(id);
	}
	
	public static List  findStudentByName(String name) {
		return new StudentDAO().findByName(name);
	}
	
	public static List  findTeacherByName(String name) {
		return new TeacherDAO().findByName(name);
	}
	
	public static void updateStudent(Student student) {
		new StudentDAO().update(student);
	}
	
	public static void updateTeacher(Teacher teacher) {
		new TeacherDAO().update(teacher);
	}
	
	public static List findStudentAll() {
		return new StudentDAO().findAll();
	}
	
	public static List findTeacherAll() {
		return new TeacherDAO().findAll();
	}
	
}
